package src.Packets;

import src.Main;
import src.Players.*;

public class MagicOnPlayer
{
    private Player p;

    public MagicOnPlayer(Player _p)
    {
        if(_p == null)
        {
            return;
        }
        p = _p;
        p.attackPlayer = p.inStream.readSignedWordA();
        p.clickId = p.inStream.readSignedWordBigEndian();
        if(!canAttackPlayer())
        {
            resetAttack();
            return;
        }
        if(p.faceToRequest != (p.attackPlayer + 32768))
        {
            p.requestFaceTo(p.attackPlayer + 32768);
        }
        if(p.combatDelay > 0)
        {
            return;
        }
        PlayerItems pi = new PlayerItems();
        Player plr = Main.m.ge.players[p.attackPlayer];
        int magicDef = random(getMagicDef(plr));
        int magicAtk = random(getMagicAtk(p));
        int hitDiff = 0;
        switch(p.clickId)
        {
            case 12891:
                if(p.skillLvl[6] < 94)
                {
                    p.frame.sendMessage(p, "You need a Magic level of 94 to cast this spell.");
                    return;
                }
                if(pi.invItemCount(p, 560) < 4 || pi.invItemCount(p, 565) < 2 || pi.invItemCount(p, 555) < 6)
                {
                    p.frame.sendMessage(p, "You do not have enough runes to cast this spell.");
                    return;
                }
                p.requestAnim(1979, 0);
                pi.deleteItem(p, 560, 0, 4);
                pi.deleteItem(p, 565, 0, 2);
                pi.deleteItem(p, 555, 0, 6);
                if(magicAtk >= magicDef)
                {
                    plr.requestGFX(369, 0);
                    hitDiff = random(30);
                    plr.appendHit(hitDiff, 0);
                    if(plr.canWalkDelay <= 0)
                    {
                        plr.canWalkDelay = 34; //About 20.6 seconds.
                    }
                }
                else
                {
                    plr.requestGFX(85, 6553600);
                }
            break;
        }
        stopMovement();
        p.combatDelay = 5;
    }

    public void stopMovement()
    {
        if(p == null)
        {
            return;
        }
        if(p.teleportToX == -1 && p.teleportToY == -1)
        {
            //p.teleportToX = p.absX;
            //p.teleportToY = p.absY;
        }
        p.newWalkCmdSteps = 0;
        p.newWalkCmdX[0] = p.newWalkCmdY[0] = 0;
        if(p.canWalkDelay <= 0)
        {
            p.canWalkDelay = 2;
        }
        //Main.m.ge.moveP.getNextPlayerMovement(p); //As nice as this is, can screw your coordsup on other peoples screens so avoid it.
    }

    public void resetAttack()
    {
        if(p == null)
        {
            return;
        }
        p.attackingNPC = false;
        p.attackingPlayer = false;
        p.attackNPC = 0;
        p.attackPlayer = 0;
    }

    public int getMagicDef(Player p)
    {
        if(p == null)
        {
            return 0;
        }
        return (int)((p.skillLvl[1] / 2) + (p.skillLvl[6] / 2) + p.equipmentBonus[8]);
    }

    public int getMagicAtk(Player p)
    {
        if(p == null)
        {
            return 0;
        }
        return (p.skillLvl[6] + p.equipmentBonus[3] + p.equipmentBonus[3]);
    }

    public boolean canAttackPlayer()
    {
        if(p == null || p.isDead)
        {
            return false;
        }
        else if(p.attackPlayer < 1 || p.attackPlayer >= Main.m.ge.maxPlayers || Main.m.ge.players[p.attackPlayer] == null)
        {
            return false;
        }
        if(!Main.m.ge.wildernessArea(p.absX, p.absY))
        {
            p.frame.sendMessage(p, "You are not in the wild.");
            return false;
        }
        Player plr = Main.m.ge.players[p.attackPlayer];
        if(plr.isDead || plr.skillLvl[3] <= 0)
        {
            //p.frame.sendMessage(p, "This NPC is already dead.");
            return false;
        }
        if(!Main.m.ge.wildernessArea(plr.absX, plr.absY))
        {
            p.frame.sendMessage(p, "This player is not in the wild.");
            return false;
        }
        return true;
    }

    private int random(int range)
    {
        return (int)(Math.random() * (range + 1));
    }
}